package com.angysnail.guava.collection;

import java.util.Collections;
import java.util.List;

import com.angysnail.entity.Person;
import com.google.common.collect.Ordering;
import com.google.common.primitives.Ints;
import com.google.common.primitives.Longs;

public class OrderingTest {
	public static void main(String[] args) {
		testCompoud(args);
	}

	public static void testCompoud(String[] args) {
		List<Person> list = Person.getSampleList();
		Ordering<Person> ageOrder = Ordering.<Person>from((p1, p2) -> {
			return Ints.compare(p1.age, p2.age);
		});
		Ordering<Person> finalOrder = ageOrder.compound((p1, p2) -> {
			return Longs.compare(p1.score, p2.score);
		});
		Collections.sort(list, finalOrder);
		list.stream().forEach(p -> {
			System.out.println(p.toString());
		});
		// ==========================
		/**
		 * <code>
		 * com.angysnail.entity.Person@7106e68e[name=科比1,age=23,male=true,score=159800]
		 * com.angysnail.entity.Person@7eda2dbb[name=科比2,age=23,male=true,score=159801]
		 * com.angysnail.entity.Person@6576fe71[name=隆多1,age=31,male=true,score=109800]
		 * com.angysnail.entity.Person@76fb509a[name=隆多2,age=31,male=true,score=109801]
		 * com.angysnail.entity.Person@300ffa5d[name=勒布朗,age=34,male=true,score=139800]
		 * com.angysnail.entity.Person@1f17ae12[name=马布里,age=39,male=true,score=39800]
		 * 
		 * 结论：先按年龄排序，再按得分排序
		 */
	}
}
